package org.example.admin.controller;


import org.apache.commons.io.FilenameUtils;
import org.example.admin.annotation.OperationLog;
import org.example.admin.mapper.GoodsMapper;
import org.example.admin.service.GoodsService;
import org.example.entity.Goods;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;


@Controller
public class InsertGoodsController {

    @Resource
    private GoodsService goodsService;



    //图片回显
    @ResponseBody
    @PostMapping("/uploadDispalyImage")
    public Map uploadImage(MultipartFile displayImage, String itemAlias) {
        String imageName = displayImage.getOriginalFilename();
        System.out.println("文件上传商品信息:" + imageName);
        System.out.println("文件上传商品类型:" + itemAlias);
        HashMap<String, String> hashMap = new HashMap<>();
        //文件上传最终路径
        String root = "D:\\Program File\\nigix-server\\nginx-1.17.2\\html\\";
        try {
            displayImage.transferTo(new File(root+itemAlias+"\\"+imageName));
        } catch (IOException e) {
            System.out.println("文件上传失败");
        }
//        data.imageUrl,_data.filePath

        hashMap.put("filePath",new String(itemAlias+"/"+imageName));
        hashMap.put("imageUrl",new String("http://localhost/"+itemAlias+"/"+imageName));

        return hashMap ;
    }


    //添加商品
    @OperationLog(value = "添加商品",requireParamInfo = true)
    @ResponseBody
    @PostMapping("/insert.do")
    public Object insertGoods(Goods goods){
        Map<String, Object> map = new HashMap<>();

        try {
            int i = goodsService.insertGoods(goods);
            map.put("code",1);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code",-1);
            return map;
        }
        return map;
    }

}
